
public class Test {
    public static void main(String[] args) throws Exception {
        Singleton s = Singleton.get();
    }
}
// 保证一个类仅有一个实例，并提供一个访问它的全局访问点。

// 饿汉式（类加载就建好，线程安全，最简单）
final class Singleton {
    private static final Singleton INSTANCE = new Singleton();

    private Singleton() {
    } // 堵死 new

    public static Singleton get() {
        return INSTANCE;
    }
}

final class Singleton2 {
    private static volatile Singleton2 instance;

    private Singleton2() {
    }

    public static Singleton2 get() {
        if (instance == null) {
            synchronized (Singleton2.class) {
                if (instance == null)
                    instance = new Singleton2();
            }
        }
        return instance;
    }
}